const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ProviderId: 0,
    ProviderNo: "由系统生成",
    ProviderName: '',
    ProviderSimpleName: '',
    ContactName: '',
    Mb: '',
    Email: '',
    IsActive: true,
    cteV: true,
    showTopTips: false,
    errorText: "错误"
  },
  changeText: function (e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
  },
  switchStop: function (e) {
    let name = e.currentTarget.dataset.name;
    let nameMap = {}
    nameMap[name] = e.detail && e.detail.value
    this.setData(nameMap)
    console.log(this.data.IsView)
  },
  compReq: function () {
    var that = this
    var stop = true
    var arr = this.data.proList
    if (this.data.ProviderName == '') {
        this.setData({
          showTopTips: true,
          errorText: '供应商名称不能为空'
        })
        setTimeout(function () {
          that.setData({
            showTopTips: false,
            errorText: ''
          })
        }, 2000)
        stop = false
        return false
    }
    // if (this.data.ProviderNo == '') {
    //   this.setData({
    //     showTopTips: true,
    //     errorText: '供应商编号不能为空'
    //   })
    //   setTimeout(function () {
    //     that.setData({
    //       showTopTips: false,
    //       errorText: ''
    //     })
    //   }, 2000)
    //   stop = false
    //   return false
    // }
    if (this.data.ContactName == '') {
      this.setData({
        showTopTips: true,
        errorText: '联系人不能为空'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
      stop = false
      return false
    }
    if (this.data.Mb == '') {
      this.setData({
        showTopTips: true,
        errorText: '联系电话不能为空'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
      stop = false
      return false
    } else {
      var a = new RegExp(/^((13[0-9])|(14[5|7|9])|(15([0-3]|[5-9]))|(166)|(17([1|3]|[5-8]))|(18[0-9])|(19[8|9]))\d{8}$/)
      console.log(a.test(this.data.Mb))
      if (!a.test(this.data.Mb)) {
        this.setData({
          showTopTips: true,
          errorText: '请输入正确的联系电话'
        })
        setTimeout(function () {
          that.setData({
            showTopTips: false,
            errorText: ''
          })
        }, 2000)
        stop = false
        return false
        }
    }
    if (this.data.Email == '') {
      this.setData({
        showTopTips: true,
        errorText: '邮箱不能为空'
      })
      setTimeout(function () {
        that.setData({
          showTopTips: false,
          errorText: ''
        })
      }, 2000)
      stop = false
      return false
    }else{
      var a = new RegExp(/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/)
      if (!a.test(this.data.Email)) {
        this.setData({
          showTopTips: true,
          errorText: '请输入正确的邮箱'
        })
        setTimeout(function () {
          that.setData({
            showTopTips: false,
            errorText: ''
          })
        }, 2000)
        stop = false
        return false
      }
    }
    if(stop){
      wx.request({
        url: app.globalData.api + 'User/Provider',
        method: "POST",
        header: {
          Authorization: app.globalData.Authorization
        },
        data: {
          ProviderId: this.data.ProviderId,
          // ProviderNo: this.data.ProviderNo,
          ProviderName: this.data.ProviderName,
          ProviderSimpleName: this.data.ProviderSimpleName,
          ContactName: this.data.ContactName,
          Mb: this.data.Mb,
          Email: this.data.Email,
          IsActive: this.data.IsActive
        },
        success: function (res) {
          console.log(res)
          if (res.data.Code) {
            wx.navigateBack({
              delta: 1
            })
          } else {
            that.setData({
              showTopTips: true,
              errorText: res.data.Error
            })
            setTimeout(function () {
              that.setData({
                showTopTips: false
              })
            })
          }
        }
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.create == 1){
      this.setData({
        cteV: false
      })
    }
    if (options.id) {
      app.req({
        url: 'User/Provider/' + options.id,
        success: function (res) {
          var data = res.data.Result[0]
          that.setData({
            ProviderId: options.id,
            ProviderNo: data.ProviderNo,
            ProviderName: data.ProviderName,
            ProviderSimpleName: data.ProviderSimpleName,
            ContactName: data.ContactName,
            Mb: data.Mb,
            Email: data.Email,
            IsActive: data.IsActive
          })
        }
      })
    } else {
      this.setData({
        ProviderId: 0
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})